home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Best of Shareware
/
Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso
/
mac
/
ZIPPED
/
DOS
/
GRAPHICS
/
POVSRC.ZIP
/
MACHINE.ZIP
/
MAC.SIT
/
textEditor.c
< prev
next >
Wrap
Text File
|
1992-05-05
|
16KB
|
765 lines
// MPW Conversion 3/17/92 Eduard [esp] Schwan
// Added MPW include headers, changed ALL the necessary 'int's
// to 'short's (frefs and vrefs), pulled out the prototypes
// into a separate TextEditor.h file for POV.C to see, added
// needed type casts, added "qd." suffix on qd globals, ..
// whew.. looks like this used to be a Think C 4.0 thingie
// (It was. jln)
/*------------------------------------------------------------------------------
Copyright 1992 POV-Team.
This source code is distributed exclusively with POV, and is subject to
the same distribution restrictions as the rest of the source code.
* Copying, distribution and legal info is in the file povlegal.doc which
* should be distributed with this file. If povlegal.doc is not available
* or for more information please contact:
*
* Drew Wells [POV-Team Leader]
* CIS: 73767,1244 Internet: 73767.1244@compuserve.com
* Phone: (213) 254-4041
* ----------------------------------------------------------------------------*/
#include <types.h> // basic types
#include <desk.h> // SystemEdit, etc.
#include <errors.h> // noErr, etc.
#include <files.h> // FSOpen, etc.
#include <fonts.h> // monaco, etc.
#include <memory.h> // NewHandle, etc.
#include <menus.h> // MenuHandle, etc.
#include <packages.h> // SFGetFile, etc.
#include <scrap.h> // ZeroScrap, etc.
#include <toolutils.h> // watchCursor, etc.
#include <windows.h> // SizeWindow, etc.
#include <stdio.h>
#include <PrintTraps.h>
#include "config.h"
#include "POVMac.h"
extern prefs_ptr_t file_settings; // changed [esp]
extern int POV_running; /* Used to determine the menu states. */
WindowRecord wRecord;
WindowPtr TextEditWindow;
TEHandle TEH;
int linesInFolder;
Rect dragRect = { 0, 0, 1024, 1024 };
MenuHandle myEditMenus[3];
ControlHandle vScroll;
Cursor editCursor;
Cursor waitCursor;
char dirty;
char window_visible;
Str255 theFileName;
short theVRefNum;
int SetUpFiles(void)
{
pStrCopy("\p", theFileName);
theVRefNum = 0;
return 0; // should be void fn!?
}
int DoFile (int item)
{
short vRef, refNum;
Str255 fn;
switch (item) {
case fmn_open:
HideWindow(TextEditWindow); /* gives a sense of closing the old file. */
if (OldFile(fn, &vRef ))
if (FSOpen(fn, vRef, &refNum)==noErr) {
if (ReadFile(refNum, TEH)) {
pStrCopy(fn, theFileName);
theVRefNum = vRef;
SetWTitle(TextEditWindow, theFileName);
dirty = 0;
}
if (FSClose(refNum)==noErr) ;
ShowWindow(TextEditWindow);
window_visible = TRUE;
TESetSelect(0, 0, TEH);
ShowSelect();
}
else FileError("\pError opening ", fn);
break;
case fmn_close:
DoFile(fmn_render); // recurse.
CloseMyWindow();
break;
case fmn_render:
if (dirty) {
ParamText("\pSave changes to ╥",
(theFileName[0]==0) ? "\pUntitled" : theFileName,
"\p╙?", "\p");
switch (Alert(AdviseAlert, 0L)) {
case aaSave:
if (theFileName[0]==0) {
fn[0] = 0;
if (!SaveAs(fn, &vRef)) return(0);
}
else if (!SaveFile(theFileName, theVRefNum)) return(0);
break;
case aaCancel: return(0);
case aaDiscard: dirty = 0;
}
}
break;
case fmn_savetext:
if (theFileName[0]==0) goto saveas;
SaveFile(theFileName, theVRefNum);
break;
case fmn_saveas:
saveas:
fn[0] = 0;
if (SaveAs(fn, &vRef )) {
pStrCopy(fn, theFileName);
theVRefNum = vRef;
SetWTitle(TextEditWindow, theFileName);
}
break;
case fmRevert:
ParamText("\pRevert to last saved version of ╥",
theFileName, "\p╙?", "\p");
switch (Alert(AdviseAlert, 0L)) {
case aaSave:
HidePen();
TESetSelect(0, (**TEH).teLength, TEH);
ShowPen();
TEDelete(TEH);
if ((theFileName[0]!=0) &&
(FSOpen(theFileName, theVRefNum, &refNum)==noErr)) {
dirty = !ReadFile(refNum, TEH);
if (FSClose(refNum)==noErr) ;
}
ShowWindow(TextEditWindow);
window_visible = TRUE;
UpdateWindow(TextEditWindow);
case aaCancel:
case aaDiscard: return(0);;
}
break;
case fmPageSetUp:
DoPageSetUp();
break;
case fmPrint:
PrintText( (**TEH).hText, (long)(**TEH).teLength, (GrafPtr)TextEditWindow,
StringWidth("\pmmmm"));
break;
case fmQuit:
if (DoFile(fmn_close))
ExitToShell();
}
return(1);
}
static Point SFGwhere = { 90, 82 };
static Point SFPwhere = { 106, 104 };
static SFReply reply;
int SaveAs (Str255 fn, short *vRef)
{
short refNum;
if (NewFile(fn, vRef))
if (!CreateFile(fn, vRef, &refNum)) {
FileError("\pError creating file ", fn);
return (0);
} else {
HLock((**TEH).hText);
if (WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength))
FileError("\pError writing file ", fn);
HUnlock((**TEH).hText);
FSClose(refNum);
dirty = 0;
return(1);
}
}
int SaveFile (Str255 fn, short vrn)
{
short refNum;
if (FSOpen(fn, vrn, &refNum) != noErr) {
FileError("\pError opening file ", fn);
return (1);
} else {
HLock((**TEH).hText);
if (WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength))
FileError("\pError writing file ", fn);
HUnlock((**TEH).hText);
dirty = 0;
FSClose(refNum);
return(1);
}
}
int NewFile (Str255 fn, short *vRef)
{
SFPutFile(SFPwhere, "\pSave file as", fn, 0L, &reply);
if (!reply.good)
return (0);
else {
pStrCopy(reply.fName, fn);
*vRef = reply.vRefNum;
return(1);
}
}
int OldFile (Str255 fn, short *vRef)
{
SFTypeList myTypes;
myTypes[0]='TEXT';
SFGetFile(SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply );
if (!reply.good)
return (0);
else {
pStrCopy(reply.fName, fn);
*vRef = reply.vRefNum;
return(1);
}
}
int CreateFile (Str255 fn, short *vRef, short *theRef)
{
OSErr io;
io=Create(fn, *vRef, kAppSignature, 'TEXT'); // changed [esp]
if ((io == noErr) || (io == dupFNErr))
io = FSOpen(fn, *vRef, theRef );
return ((io == noErr) || (io == dupFNErr));
}
int WriteFile (short refNum, char *p, long num)
{
OSErr io;
/* we should check the return code for errors */
io=FSWrite(refNum, &num, p);
if (io)
return(io);
io=SetEOF(refNum, num);
return(io);
}
int ReadFile (short refNum, TEHandle textH)
{
char buffer[256];
long count;
OSErr io;
(**textH).selStart = 0;
(**textH).selEnd = (**textH).teLength;
TEDelete(textH);
GetEOF(refNum, &count);
if (count > 32767L)
return (999);
do {
count = 256;
io = FSRead(refNum, &count, &buffer);
TEInsert(&buffer, count, textH);
} while (io==noErr);
return (io == eofErr);
}
int pStrCopy (StringPtr p1, StringPtr p2)
/* copies a pascal string from p1 to p2 */
{
register int len = *p1; // added asignment
*(p2++) = *(p1++); // added parens
while (--len>=0) *(p2++)=*(p1++); // added parens
return 0; // should be void fn!?
}
int FileError(Str255 s, Str255 f)
{
ParamText(s, f,"\p", "\p");
Alert(ErrorAlert, 0L);
return 0; // should be void fn!?
}
pascal void ScrollProc (ControlHandle theControl, short theCode);
void PreInitWindows(void)
{
if (TextEditWindow == 0)
{
SetUpWindows();
HideWindow(TextEditWindow); /* just in case it's already visible */
}
}
int SetUpWindows(void)
{
Rect viewRect;
Rect vScrollRect;
SetPort((TextEditWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1L)));
TextFont(monaco);
TextSize(9);
vScrollRect = (*TextEditWindow).portRect;
vScrollRect.left = vScrollRect.right-15;
vScrollRect.right += 1;
vScrollRect.bottom -= 14;
vScrollRect.top -= 1;
vScroll = NewControl( TextEditWindow, &vScrollRect, "\p", 1, 0, 0, 0,
scrollBarProc, 0L);
viewRect = qd.thePort->portRect;
viewRect.right -= SBarWidth;
viewRect.bottom -= SBarWidth;
InsetRect(&viewRect, 4, 4);
TEH = TENew(&viewRect, &viewRect);
SetView((WindowPtr) qd.thePort);
dirty = 0;
return 0; // should be void fn!?
}
int AdjustText (void)
{
int oldScroll, newScroll, delta;
oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
delta = oldScroll - newScroll;
if (delta != 0)
TEScroll(0, delta, TEH);
SetVScroll();
return 0; // should be void fn!?
}
int SetVScroll(void)
{
register int n;
n = (**TEH).nLines-linesInFolder;
if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
n++;
SetCtlMax(vScroll, n > 0 ? n : 0);
return 0; // should be void fn!?
}
int ShowSelect (void)
{
register int topLine, bottomLine, theLine;
SetVScroll();
AdjustText();
topLine = GetCtlValue(vScroll);
bottomLine = topLine + linesInFolder;
if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
(**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
;
SetCtlValue(vScroll, theLine - linesInFolder / 2);
AdjustText();
}
return 0; // should be void fn!?
}
int SetView (WindowPtr w)
{
(**TEH).viewRect = w->portRect;
(**TEH).viewRect.right -= SBarWidth;
(**TEH).viewRect.bottom -= SBarWidth;
InsetRect(&(**TEH).viewRect, 4, 4);
linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
(**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
(**TEH).destRect.right = (**TEH).viewRect.right;
TECalText(TEH);
return 0; // should be void fn!?
}
int UpdateWindow (WindowPtr theWindow)
{
GrafPtr savePort;
GetPort(&savePort);
SetPort(theWindow);
BeginUpdate(theWindow);
EraseRect(&theWindow->portRect);
DrawControls(theWindow);
DrawGrowIcon(theWindow);
TEUpdate(&theWindow->portRect, TEH);
EndUpdate(theWindow);
SetPort(savePort);
return 0; // should be void fn!?
}
pascal void ScrollProc (ControlHandle theControl, short theCode)
{
int pageSize;
int scrollAmt;
int oldCtl;
if (theCode == 0)
return ;
pageSize = ((**TEH).viewRect.bottom-(**TEH).viewRect.top) /
(**TEH).lineHeight - 1;
switch (theCode) {
case inUpButton:
scrollAmt = -1;
break;
case inDownButton:
scrollAmt = 1;
break;
case inPageUp:
scrollAmt = -pageSize;
break;
case inPageDown:
scrollAmt = pageSize;
break;
}
oldCtl = GetCtlValue(theControl);
SetCtlValue(theControl, oldCtl+scrollAmt);
AdjustText();
}
int DoContent(WindowPtr theWindow, EventRecord *theEvent)
{
short cntlCode;
ControlHandle theControl;
GrafPtr savePort;
GetPort(&savePort);
SetPort(theWindow);
GlobalToLocal(&theEvent->where);
if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
if (PtInRect(theEvent->where, &(**TEH).viewRect))
TEClick(theEvent->where, (theEvent->modifiers & shiftKey)!=0, TEH);
} else if (cntlCode == inThumb) {
TrackControl(theControl, theEvent->where, 0L);
AdjustText();
} else
TrackControl(theControl, theEvent->where, (ProcPtr)&ScrollProc);
SetPort(savePort);
return 0; // should be void fn!?
}
void MyGrowWindow(WindowPtr w, Point p)
{
GrafPtr savePort;
long theResult;
Rect oldHorizBar;
Rect r;
GetPort(&savePort);
SetPort(w);
oldHorizBar = w->portRect;
oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
theResult = GrowWindow(w, p, &r);
if (theResult == 0)
return;
SizeWindow( w, LoWord(theResult), HiWord(theResult), false);
get_WindowPos(w, &file_settings->srcWind_pos); // added [esp]
// file_settings->src_width = LoWord(theResult); -- removed [esp]
// file_settings->src_height = HiWord(theResult); -- removed [esp]
// WriteFileSettings(); -- moved to MyCloseWindow [esp]
InvalRect(&w->portRect);
SetView(w);
EraseRect(&oldHorizBar);
MoveControl(vScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
SizeControl(vScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
r = (**vScroll).contrlRect;
ValidRect(&r);
SetVScroll();
AdjustText();
SetPort(savePort);
}
int CloseMyWindow(void)
{
window_visible = FALSE;
HideWindow(TextEditWindow);
TESetSelect(0, (**TEH).teLength, TEH);
TEDelete(TEH);
SetVScroll();
SetUpFiles();
WriteFileSettings(); /* Save window position */ // added [esp]
CloseFileSettings(); // added [esp]
return 0; // should be void fn!?
}
#define ours(w) ((TextEditWindow != NULL) && (w == TextEditWindow))
PleaseWait()
{
SetCursor(&waitCursor);
}
main_init()
{
TextEditWindow = 0;
SetUpFiles();
SetUpCursors();
PreInitWindows();
}
int DoEditMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
{
switch (windowPart) {
case inGoAway:
if (ours(whichWindow) && (POV_running == 0))
if (TrackGoAway(TextEditWindow, myEvent->where))
DoFile(fmn_close);
break;
case inDrag:
if (ours(whichWindow))
{ // added [esp]
DragWindow(whichWindow, myEvent->where, &dragRect);
get_WindowPos(whichWindow, &file_settings->srcWind_pos); // added [esp]
} // added [esp]
break;
case inGrow:
if (ours(whichWindow))
MyGrowWindow(whichWindow, myEvent->where);
break;
case inContent:
if (whichWindow != FrontWindow())
SelectWindow(whichWindow);
else if (ours(whichWindow))
DoContent(whichWindow, myEvent);
break;
}
return 0; // should be void fn!?
}
int MaintainCursor(void)
{
Point pt;
WindowPeek wPtr;
GrafPtr savePort;
if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
GetPort(&savePort);
SetPort((GrafPtr)wPtr);
GetMouse(&pt);
if (PtInRect(pt, &(**TEH).viewRect ) )
SetCursor( &editCursor);
else SetCursor(&qd.arrow);
SetPort(savePort);
}
return 0; // should be void fn!?
}
int SetUpCursors(void)
{
CursHandle hCurs;
hCurs = GetCursor(1);
editCursor = **hCurs;
hCurs = GetCursor(watchCursor);
waitCursor = **hCurs;
return 0; // should be void fn!?
}
#define topMargin 20
#define leftMargin 20
#define bottomMargin 20
#define tabChar ((char)'\t')
static THPrint hPrint = NULL;
static int tabWidth;
/**
** Prototypes for private functions.
** (They really should be static.)
**
**/
int CheckPrintHandle(void);
int MyDrawText(char *p, int count);
int PrDoc(char **hText, long count, THPrint hPrint, int font, int size);
int HowMany(void);
CheckPrintHandle()
{
if (hPrint==NULL)
PrintDefault(hPrint = (TPrint **) NewHandle(sizeof( TPrint)));
}
DoPageSetUp()
{
PrOpen();
CheckPrintHandle();
if (PrStlDialog(hPrint)) ;
PrClose();
}
MyDrawText(char *p, int count)
{
register char *p1, *p2;
int len;
Point pt;
p1 = p;
p2 = p+count;
while (p<p2) {
while ((p1<p2) && (*p1 !=tabChar)) *p1++;
if ((len=p1-p)>0) DrawText(p, 0, p1-p);
if (*p1==tabChar) {
GetPen(&pt);
Move((tabWidth-(pt.h-leftMargin)%tabWidth), 0);
*p1++;
}
p = p1;
}
}
PrDoc (char **hText, long count, THPrint hPrint, int font, int size)
{
register int line = 0;
register int lastLineOnPage = 0;
int length;
Rect printRect;
int linesPerPage;
int lineBase;
int lineHeight;
register char *ptr, *p1;
FontInfo info;
TPPrPort printPort;
printPort = PrOpenDoc(hPrint, 0L, 0L);
SetPort((GrafPtr)printPort);
TextFont(font);
TextSize(size);
printRect = (**hPrint).prInfo.rPage;
GetFontInfo(&info);
lineHeight = info.leading+info.ascent+info.descent;
linesPerPage =
(printRect.bottom-printRect.top-topMargin-bottomMargin)/lineHeight;
HLock(hText);
ptr = p1 = (*hText);
do {
PrOpenPage(printPort, 0L);
lastLineOnPage += linesPerPage;
MoveTo( printRect.left+leftMargin,
(lineBase = printRect.top+lineHeight) );
do {
/* PrintLine: */
while ((ptr<=(*hText)+count) && (*ptr++ != (char)'\r')) ;
if ((length=(int)(ptr-p1)-1)>0)
MyDrawText(p1, length);
MoveTo( printRect.left+leftMargin, (lineBase += lineHeight));
p1 = ptr;
} while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
PrClosePage(printPort);
} while (ptr<(*hText)+count);
HUnlock(hText);
PrCloseDoc(printPort);
}
PrintText(char **hText, long length, GrafPtr gp, int tabPixels)
{
GrafPtr savePort;
TPrStatus prStatus;
int copies;
PrOpen();
CheckPrintHandle();
tabWidth = tabPixels;
SetCursor(&qd.arrow);
if (PrJobDialog(hPrint) != 0) {
PleaseWait();
GetPort(&savePort);
for (copies=HowMany(); copies>0; copies--) {
PrDoc (hText, length, hPrint, (*gp).txFont, (*gp).txSize);
PrPicFile(hPrint, 0L, 0L, 0L, &prStatus);
}
SetPort(savePort);
}
PrClose();
}
int HowMany(void)
{
return( ((**hPrint).prJob.bJDocLoop==bDraftLoop) ?
(**hPrint).prJob.iCopies : 1 );
}